#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        vector<vector<bool>> visit(grid.size(), vector<bool>(grid[0].size(), false));
        int ret = 0;
        for (int i = 0; i < grid.size(); i++)
            for (int j = 0; j < grid[0].size(); j++)
                if (!visit[i][j])
                {
                    visit[i][j] = true;
                    if (grid[i][j] == '1')
                        ret++, FF(grid, i, j, visit);
                }
        return ret;
    }
    void FF(vector<vector<char>>& grid, int sr, int sc, vector<vector<bool>>& visit)
    {
        queue<pair<int, int>> q;
        q.push({ sr, sc });
        while (q.size())
        {
            auto [r, c] = q.front(); q.pop();
            static int dx[4] = { 0,0,1,-1 };
            static int dy[4] = { 1,-1,0,0 };
            for (int i = 0; i < 4; i++)
            {
                int x = r + dx[i];
                int y = c + dy[i];
                if (x >= 0 && y >= 0 && x < grid.size() && y < grid[0].size()
                    && visit[x][y] == false && grid[x][y] == '1')
                    q.push({ x, y }), visit[x][y] = true;
            }
        }
    }
};